package com.example.zjx.mvp_logindemo.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zjx.mvp_logindemo.R;
import com.example.zjx.mvp_logindemo.bean.User;
import com.example.zjx.mvp_logindemo.otto.AppBus;
import com.example.zjx.mvp_logindemo.presenter.UserLoginPresent;
import com.example.zjx.mvp_logindemo.view.IUserLoginView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by zjx
 * information 登录界面
 */
public class LoginActivity extends AppCompatActivity implements IUserLoginView{
    //登录输入框
    private EditText login_username_edit;
    //密码输入框
    private EditText login_passwd_edit;
    //用户登录桥梁
    private UserLoginPresent userLoginPresent=new UserLoginPresent(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        //注册事件总线
        AppBus.getInstance().register(this);
        EventBus.getDefault().register(this);
    }
    //初始化组件
    public void initView(){
        login_username_edit=findViewById(R.id.login_username_edit);
        login_passwd_edit=findViewById(R.id.login_passwd_edit);
    }


    @Override
    public LoginActivity getActivity() {
        return this;
    }

    @Override
    public String getUsername() {

        return login_username_edit.getText().toString().trim();
    }

    @Override
    public String getUserpass() {
        return login_passwd_edit.getText().toString().trim();
    }

    @Override
    public void passwdclear() {
        login_passwd_edit.getText().clear();
    }




    public void loginin(View view) {
        userLoginPresent.userLogin();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销事件总线
        AppBus.getInstance().unregister(this);
        EventBus.getDefault().unregister(this);
    }



    //登陆成功返回结果
    @org.greenrobot.eventbus.Subscribe(threadMode = ThreadMode.MAIN)
    public void loginUser(User user){
        Toast.makeText(getApplicationContext(),""+user.toString(),Toast.LENGTH_SHORT).show();
    }
    //弹出提示
    @org.greenrobot.eventbus.Subscribe(threadMode = ThreadMode.MAIN)
    public void loginmessage(String message){
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
    }
//     //登陆成功返回结果
//    @Subscribe
//    public void loginUser(User user){
//        Toast.makeText(getApplicationContext(),""+user.toString(),Toast.LENGTH_SHORT).show();
//    }
//    //弹出提示
//    @Subscribe
//    public void loginUser(String message){
//        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
//    }
}
